package com.googlecode.spreadit.activity;

import static com.googlecode.spreadit.bean.SpreadRule.ATTRIBUTE_ACTIVE;
import static com.googlecode.spreadit.bean.SpreadRule.ATTRIBUTE_NAME;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.googlecode.spreadit.R;
import com.googlecode.spreadit.bean.SpreadRule;
import com.googlecode.spreadit.matching.SpreadRuleBase;

/**
 * This {@link ListActivity} relates to the {@link R.layout#edit_rule
 * "Edit Rule" screen}.
 * 
 * @author Markus L. Dechert (markus.dechert@googlemail.com)
 */
public class EditRule extends Activity {

	private final String LOG_TAG = getClass().getSimpleName();

	public static final String HANDLER_ID = "com.googlecode.spreadit.activity.EDIT_RULE";

	private EditText vName;

	private CheckBox vActive;

	private TextView vMatchIfExplained;

	private TextView vReceiversExplained;

	private SpreadRule rule;

	/** Called when the activity is first created. */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.edit_rule);

		long spreadRuleId = getIntent().getLongExtra(SpreadRule.ATTRIBUTE_ID,
				-1L);
		SpreadRuleBase srb = SpreadRuleBase.getInstance();
		rule = srb.getRuleById(spreadRuleId);

		// Retrieve views once and populate them with the rule's values
		resolveWidgetViews();
		populateViews(rule);

		// Bind save button
		Button saveButton = (Button) findViewById(R.id.edit_rule_buttonDone);
		saveButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				saveValues();
				finish();
			}
		});

		// Bind edit spread condition
		ImageButton editConditionButton = (ImageButton) findViewById(R.id.edit_rule_editMatchIfExpression);
		editConditionButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				Intent manageConditionsScreen = new Intent(
						ManageSpreadConditions.HANDLER_ID);
				manageConditionsScreen.putExtra(SpreadRule.ATTRIBUTE_ID,
						rule.getId());
				startActivity(manageConditionsScreen);
			}
		});

		// TODO: Validations (see
		// http://stackoverflow.com/questions/5218691/how-to-display-input-errors-in-popup)

	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);

		outState.putString(ATTRIBUTE_NAME, vName.getText().toString());
		outState.putBoolean(ATTRIBUTE_ACTIVE, vActive.isChecked());
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);

		vName.setText(savedInstanceState.getString(ATTRIBUTE_NAME));
		vActive.setChecked(savedInstanceState.getBoolean(ATTRIBUTE_ACTIVE));
		// vTargetChannel.setSelection(savedInstanceState
		// .getInt(ATTRIBUTE_TARGET_CHANNEL));
	}

	/**
	 * Since we're accessing the views a lot we'll just memorize their
	 * references in order to avoid too many {@link Activity#findViewById(int)}
	 * calls.
	 */
	private void resolveWidgetViews() {
		vName = (EditText) findViewById(R.id.edit_rule_inputName);
		vActive = (CheckBox) findViewById(R.id.edit_rule_inputActive);
		vMatchIfExplained = (TextView) findViewById(R.id.edit_rule_matchIfExpressionExplained);
		vReceiversExplained = (TextView) findViewById(R.id.edit_rule_captionTargetReceiversExplained);
		// vTargetChannel = (Spinner)
		// findViewById(R.id.edit_rule_inputTargetChannel);
	}

	/**
	 * This method populates the views in this screen with the values given in
	 * the {@link SpreadRule}.
	 * 
	 * @param rule
	 *            the {@link SpreadRule} with the values to use.
	 */
	private void populateViews(SpreadRule rule) {
		vName.setText(rule.getName());
		vActive.setChecked(rule.isActive());
		vMatchIfExplained.setText(rule.getMatchIf().toString());
		vReceiversExplained.setText(rule.getTargetReceiversAsString());

		// for (int i = 0; i < vTargetChannel.getCount(); i++) {
		// Object o = vTargetChannel.getAdapter().getItem(i);
		// if (o.toString().equalsIgnoreCase(
		// rule.getTargetChannel().toString())) {
		// vTargetChannel.setSelection(i);
		// break;
		// }
		// }
	}

	protected void saveValues() {
		rule.setName(vName.getText().toString());
		rule.setActive(vActive.isChecked());

		// final String targetChannel = vTargetChannel.getSelectedItem()
		// .toString();
		// rule.setTargetChannel(SpreadChannel.getByTitle(targetChannel));

	}

}
